<?php
/**
 * Created by PhpStorm.
 * User: luojinyi
 * Date: 2018/3/10
 * Time: 下午5:18
 */

namespace App\Http\Controllers\Api\Permission;

use App\Models\User;
use App\Services\Admin\PermissionService;
use App\Http\Transformers\AllPermissionTransformer;
use App\Http\Transformers\CurrentPermissionTransformer;
use Illuminate\Support\Facades\Auth;
use App\Http\Transformers\PermissionTransformer;
use App\Http\Transformers\PermissionsTransformer;
use App\Http\Requests\Permission\AddPermissionRouterRequest;
use App\Http\Requests\Permission\UpdateRequest;
use App\Http\Requests\Permission\MenuRequest;
use App\Http\Requests\Permission\ShowRequest;
use App\Http\Requests\Permission\SingleRequest;
use App\Http\Requests\Permission\PermissionToUserRequest;
use App\Http\Requests\Permission\AllRequest;
use App\Http\Requests\Permission\DeleteRequest;
use App\Http\Controllers\Api\BaseAdminController;

/**
 * 权限管理
 * 该控制器只做两件事情：1、接收参数 2、dingo返回响应及其数据
 * 其他数据处理、外部服务、数据库查询等剥离至 Service和 Repository
 * Class PermissionController
 * @package App\Http\Controllers\Api\Permission
 */
class PermissionController extends BaseAdminController
{
    /**
     * @var PermissionService 注入的PermissionServices 外部服务
     */
    protected $permissionServices;

    /**
     * PermissionController constructor.
     * @param PermissionService $permissionServices
     */
    public function __construct(PermissionService $permissionServices){
        $this->permissionServices = $permissionServices;
    }

    /**
     * @api {get} /app/paper/list 获取路由权限列表
     * @apiDescription   获取路由权限列表
     * @apiGroup permission
     * @apiPermission none
     * @apiVersion 0.1.0
     * @apiParam {Int}      [limit]    每页显示条数
     * @apiParam {Int}      [type]    类型
     * @apiParam {String}  [name]    名称
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *   {
     *       "data": [
     *           {
     *               "id": 364,
     *               "name": "名称",
     *           },
     *           {
     *               "id": 364,
     *               "name": "名称",
     *           }
     *       ],
     *       "meta": {
     *       "pagination": {
     *           "total": 2,
     *           "count": 2,
     *           "per_page": 15,
     *           "current_page": 1,
     *           "total_pages": 1,
     *           "links": null
     *       }
     *       }
     *   }
     * @apiErrorExample {json} Error-Response:
     *     HTTP/1.1 400 Bad Request
     * @param ShowRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function pagesRouting(ShowRequest $request){
        $data = $request->all();

        $arrData = $this->permissionServices->handleCondition($data);
        $limit = data_get($data, 'limit', 15);

        $page = $this->permissionServices->getPermissionRouterPage($arrData, $limit);

        return $this->response->paginator($page, new PermissionTransformer());
    }

    /**
     * 添加权限路由
     */
    public function addRouting(AddPermissionRouterRequest $request){
        $add = $this->permissionServices->addPermissionRouter($request->all());

        if($add){
            return $this->response->created('', 'success');
        }
        $this->response->errorInternal('新增失败，请重试！');
    }

    /**
     * 更新权限路由只能更新描述 名称在其它地方有关联
     */
    public function updateRouting(UpdateRequest $request){
        $update = $this->permissionServices->updatePermissionRouter($request->all());

        if($update){
            return $this->response->created('', 'success');
        }
        $this->response->errorInternal('更新失败，请重试！');
    }

    /**
     * 删除路由权限
     */
    public function deleteRouting(DeleteRequest $request){
        $ids = $request->get('ids');

        if( $this->permissionServices->deletePermissionRouter($ids) ){
            return $this->response->created('', '删除成功');
        }
        $this->response->errorInternal('删除失败，请重试！');
    }

    /**
     * 获取当前用户权限
     */
    public function current(MenuRequest $request){
        $data = $this->permissionServices->currentPermission();

        return $this->response->item($data, new CurrentPermissionTransformer());
    }

    /**
     * 获取所有权限路由列表
     */
    public function choice(AllRequest $request){
        $data = $request->all();
        $arrData = $this->permissionServices->handleCondition($data);

        $allData = $this->permissionServices->choicePermission($arrData);

        return $this->response->collection($allData, new AllPermissionTransformer());
    }

    /**
     * 获取单条权限路由数据
     */
    public function single(SingleRequest $request){
        $id = $request->get('id');

        $data = $this->permissionServices->singlePermission($id);

        return $this->response->item($data, new PermissionTransformer());
    }

    /**
     * 结构性数据
     * @return \Dingo\Api\Http\Response
     */
    public function structure(){

        $data = $this->permissionServices->structurePermission();

        return $this->response->collection($data, new PermissionTransformer());
    }

}
